﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for ErrorMessage.xaml
    /// </summary>
    public partial class Message : Window
    {
        public static Message errormsg
        {
            private set;
            get;
        }
        public static void ShutdownWaitingCurrent()
        {
            if (errormsg != null)
                if ((errormsg.MessageType & MessageType.Wait) == MessageType.Wait)
                {
                    errormsg.RestoreWindowStatus();
                    errormsg.Close();
                    errormsg = null;
                }
        }
        public MessageType MessageType
        {
            get;
            set;
        }
        public static MessageType Show(Window sender, string title, MessageType msgType, string content, string summary)
        {
            if (errormsg != null)
            {
                throw new ApplicationException();
            }
            errormsg = new Message();
            errormsg.MessageType = msgType;
            errormsg.Owner = sender;
            if (sender != null)
            {
                errormsg.Height = (sender).ActualHeight;
                errormsg.Width = (sender).ActualWidth;
            }
            errormsg.Title = title;
            errormsg.Summary.Text = summary;
            errormsg.Content.Text = content;
            if ((msgType & MessageType.Comfirm) == MessageType.Comfirm)
            {
                errormsg.OK.Visibility = Visibility.Visible;
            }
            if ((msgType & MessageType.Cancel) == MessageType.Cancel)
            {
                errormsg.Cancel.Visibility = Visibility.Visible;
            }
            if ((msgType | MessageType.Wait) == MessageType.Wait)
            {
                errormsg.progressBar.Visibility = Visibility.Visible;
            }
            if (sender != null)
            {
                errormsg.SetWindowStatus();
                sender.Topmost = true;
            }
            errormsg.ShowDialog();
            if (msgType != MessageType.Wait)
            {
                var result = errormsg.Result;
                errormsg = null;
                return result;
            }
            else
            {
                return MessageType._UnSpecified_;
            }
        }
        private Message()
        {
            this.InitializeComponent();
            Result = Test.MessageType._UnSpecified_;
            // Insert code required on object creation below this point.
        }
        public MessageType Result
        {
            get;
            set;
        }
        void RestoreWindowStatus()
        {
            if (this.Owner == null)
            {
                return;
            }
            this.Owner.Effect = null;
            this.Owner.Topmost = false;
        }
        void SetWindowStatus()
        {
            (Resources["OnLoaded1"] as System.Windows.Media.Animation.Storyboard).Completed += new EventHandler(ErrorMessage_Completed);
            (Resources["OnLoaded1"] as System.Windows.Media.Animation.Storyboard).Begin();
        }

        void ErrorMessage_Completed(object sender, EventArgs e)
        {
            if (errormsg == null) return;
            Owner.Effect = new System.Windows.Media.Effects.BlurEffect() { Radius = 10 };
        }
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageType.Cancel;
            RestoreWindowStatus();
            this.Close();
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageType.Comfirm;
            RestoreWindowStatus();
            this.Close();
        }
    }
    [Flags]
    public enum MessageType : uint
    {
        _UnSpecified_ = 0,
        Cancel = 1,
        Comfirm = 2,
        Wait = 4
    }
}